﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;

namespace TimKen.DataAccess
{
    public   class AdminActionDAO
    {
        public AdminActionDAO()
        { }
        #region  成员方法
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int ActionID)
        {
            int rowsAffected;
            SqlParameter[] parameters = {
					new SqlParameter("@ActionID", SqlDbType.Int,4)};
            parameters[0].Value = ActionID;

            int result = DbHelperSQL.RunProcedure("UP_AdminAction_Exists", parameters, out rowsAffected);
            if (result == 1)
            {
                return true;
            }
            else
            {
              
                return false;
            }
        }

        /// <summary>
        ///  增加一条数据
        /// </summary>
        public int Add(TimKen.DataEntity.AdminAction model)
        {
            int rowsAffected;
            SqlParameter[] parameters = {
					new SqlParameter("@ActionID", SqlDbType.Int,4),
					new SqlParameter("@ParentID", SqlDbType.Int,4),
					new SqlParameter("@ActionCodeEn", SqlDbType.NVarChar,20),
					new SqlParameter("@ActionCodeCn", SqlDbType.NVarChar,50)};
            parameters[0].Direction = ParameterDirection.Output;
            parameters[1].Value = model.ParentID;
            parameters[2].Value = model.ActionCodeEn;
            parameters[3].Value = model.ActionCodeCn;

            DbHelperSQL.RunProcedure("UP_AdminAction_ADD", parameters, out rowsAffected);
            return (int)parameters[0].Value;
        }

        /// <summary>
        ///  更新一条数据
        /// </summary>
        public void Update(TimKen.DataEntity.AdminAction model)
        {
            int rowsAffected;
            SqlParameter[] parameters = {
					new SqlParameter("@ActionID", SqlDbType.Int,4),
					new SqlParameter("@ParentID", SqlDbType.Int,4),
					new SqlParameter("@ActionCodeEn", SqlDbType.NVarChar,20),
					new SqlParameter("@ActionCodeCn", SqlDbType.NVarChar,50)};
            parameters[0].Value = model.ActionID;
            parameters[1].Value = model.ParentID;
            parameters[2].Value = model.ActionCodeEn;
            parameters[3].Value = model.ActionCodeCn;

            DbHelperSQL.RunProcedure("UP_AdminAction_Update", parameters, out rowsAffected);
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public void Delete(int ActionID)
        {
            int rowsAffected;
            SqlParameter[] parameters = {
					new SqlParameter("@ActionID", SqlDbType.Int,4)};
            parameters[0].Value = ActionID;

            DbHelperSQL.RunProcedure("UP_AdminAction_Delete", parameters, out rowsAffected);
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public TimKen.DataEntity.AdminAction GetModel(int ActionID)
        {
            SqlParameter[] parameters = {
					new SqlParameter("@ActionID", SqlDbType.Int,4)};
            parameters[0].Value = ActionID;

            TimKen.DataEntity.AdminAction model = new TimKen.DataEntity.AdminAction();
            DataSet ds = DbHelperSQL.RunProcedure("UP_AdminAction_GetModel", parameters, "ds");
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["ActionID"].ToString() != "")
                {
                    model.ActionID = int.Parse(ds.Tables[0].Rows[0]["ActionID"].ToString());
                }
                if (ds.Tables[0].Rows[0]["ParentID"].ToString() != "")
                {
                    model.ParentID = int.Parse(ds.Tables[0].Rows[0]["ParentID"].ToString());
                }
                model.ActionCodeEn = ds.Tables[0].Rows[0]["ActionCodeEn"].ToString();
                model.ActionCodeCn = ds.Tables[0].Rows[0]["ActionCodeCn"].ToString();
                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ActionID,ParentID,ActionCodeEn,ActionCodeCn ");
            strSql.Append(" FROM AdminAction ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }
      

        #endregion  成员方法
    }
}
